#include <retro.h>
#include "retro.creator.h"

using namespace Retro::Creator::Dialog;
using namespace SPLib::Windows;

VOID  EditGridMap::loadTilesets(INT selectedID)
{
  const Generic::Vector<Data::Resource*>& wResources =
    Frame::get()->getProject().getResources();

  TCHAR szText[RETRO_MAXTEXT];
  INT selected = 0;

  ComboBox_ResetContent(hComboTileset);

  for (UINT i = 0; i < wResources.getLength(); ++i)
  {
    if (wResources[i]->getType() == Data::ResourceTexture)
    {
      if (((Data::Texture*)wResources[i])->getTextureType() ==
        Data::TextureTileset)
      {
        _stprintf(szText, _T("%.4d - %s"),
          wResources[i]->getID(),
          wResources[i]->getName());

        INT id = ComboBox_AddString(hComboTileset, szText);

        ComboBox_SetItemData(hComboTileset, id, wResources[i]);

        if (wResources[i]->getID() == selectedID)
          selected = id;
      }
    }
  }

  ComboBox_SetCurSel(hComboTileset, selected);
}

VOID  EditGridMap::onUnload()
{
}

VOID  EditGridMap::onLoad()
{
  addHeader(RETRO_GRIDMAP_MAXWIDTH, RETRO_GRIDMAP_MAXHEIGHT);
  addComboBox(hComboTileset, lblTileset, _T("Tileset: "));
  addFooter();
}

VOID  EditGridMap::newMap()
{
  TCHAR szName[RETRO_MAXTEXT];

  _stprintf(szName, _T("Grid Map %.4d"),
    Frame::get()->getProject().getNextResourceID(Data::ResourceGridMap));

  SendMessage(hUpDownWidth, UDM_SETPOS32, 0, RETRO_GRIDMAP_DEFWIDTH);
  SendMessage(hUpDownHeight, UDM_SETPOS32, 0, RETRO_GRIDMAP_DEFHEIGHT);
  SendMessage(hUpDownLayers, UDM_SETPOS32, 0, RETRO_GRIDMAP_DEFLAYERS);

  SetWindowText(hEditName, szName);
  loadTilesets(0);
  setText(_T("New Grid Map"));
}

VOID  EditGridMap::setMap(Data::GridMap* value)
{
  Editor::GridEditor* wEditor = (Editor::GridEditor*)
    Frame::get()->findEditor(value);

  if (wEditor)
  {
    SendMessage(hUpDownWidth,   UDM_SETPOS32, 0, wEditor->getMapWidth());
    SendMessage(hUpDownHeight,  UDM_SETPOS32, 0, wEditor->getMapHeight());
    SendMessage(hUpDownLayers,  UDM_SETPOS32, 0, wEditor->getMapLayers());
  }
  else
  {
    SendMessage(hUpDownWidth, UDM_SETPOS32, 0, value->getWidth());
    SendMessage(hUpDownHeight, UDM_SETPOS32, 0, value->getHeight());
    SendMessage(hUpDownLayers, UDM_SETPOS32, 0, value->getLayers());
  }

  SetWindowText(hEditName, value->getName());
  loadTilesets(value->getTilesetID());
  setText(_T("Grid Map Properties"));
}

VOID  EditGridMap::getMap(Data::GridMap* value)
{
  Editor::GridEditor* wEditor = (Editor::GridEditor*)
    Frame::get()->findEditor(value);

  Data::Texture* tileset = NULL;

  if (ComboBox_GetCount(hComboTileset) > 0)
  {
    tileset = (Data::Texture*)ComboBox_GetItemData(
      hComboTileset,
      ComboBox_GetCurSel(hComboTileset));
  }

  TCHAR szName[RETRO_MAXNAME];
  Edit_GetText(hEditName, szName, RETRO_MAXNAME);

  value->setName(szName);

  if (wEditor)
  {
    wEditor->setBufferSize(
      (INT)SendMessage(hUpDownWidth,  UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownHeight, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownLayers, UDM_GETPOS32, 0, NULL));

    if (tileset > 0)
      wEditor->setTileset(tileset->getID());
  }
  else
  {
    value->setSize(
      (INT)SendMessage(hUpDownWidth, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownHeight, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownLayers, UDM_GETPOS32, 0, NULL));

    if (tileset > 0)
      value->setTilesetID(tileset->getID());
  }
}
